package com.ruoyi.repairGroup.service.impl;

import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.HouseRepairCheckT;
import com.ruoyi.repairArea.domain.HouseRepairAreaT;
import com.ruoyi.repairArea.mapper.HouseRepairAreaTMapper;
import com.ruoyi.repairDoor.domain.HouseRepairDoorT;
import com.ruoyi.repairDoor.mapper.HouseRepairDoorTMapper;
import com.ruoyi.repairGroup.service.IHouseRepairDoorTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 户资金修改Service业务层处理
 *
 * @author tyq
 * @date 2023-05-24
 */
@Service
public class HouseRepairDoorTServiceImpl implements IHouseRepairDoorTService
{
    @Autowired
    private HouseRepairDoorTMapper houseRepairDoorTMapper;
    @Autowired
    private HouseRepairAreaTMapper houseRepairAreaTMapper;

    /**
     * 查询户资金修改
     *
     * @param doorId 户资金修改主键
     * @return 户资金修改
     */
    @Override
    public HouseRepairDoorT selectHouseRepairDoorTByDoorId(Long doorId)
    {
        return houseRepairDoorTMapper.selectHouseRepairDoorTByDoorId(doorId);
    }

    @Override
    public HouseRepairDoorT getProjectAreaName(String projectId) {
        return houseRepairDoorTMapper.getProjectAreaName(projectId);
    }

    /**
     * 查询户资金修改列表
     *
     * @param houseRepairDoorT 户资金修改
     * @return 户资金修改
     */
    @Override
    public List<HouseRepairDoorT> selectHouseRepairDoorTList(HouseRepairDoorT houseRepairDoorT)
    {
        return houseRepairDoorTMapper.selectHouseRepairDoorTList(houseRepairDoorT);
    }

    @Override
    public List<HouseRepairDoorT> selectHouseRepairTListByHouseId(String houseId)
    {
        return houseRepairDoorTMapper.selectHouseRepairTListByHouseId(houseId);
    }

    @Override
    public HouseRepairDoorT selectHouseRepairDoorSum(HouseRepairDoorT houseRepairDoorT)
    {
        return houseRepairDoorTMapper.selectHouseRepairDoorSum(houseRepairDoorT);
    }

    /**
     * 新增户资金修改
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHouseRepairDoorT( String[] houseIds, String projectId)
    {
        int result = 0;
        for(String houseId : houseIds){
            HouseRepairDoorT houseRepairDoorVO=new HouseRepairDoorT();
            houseRepairDoorVO.setProjectId(projectId);
            houseRepairDoorVO.setHouseId(houseId);
            int id = houseRepairDoorTMapper.getRepairId();
            houseRepairDoorVO.setRepairId(id+1);
            result = houseRepairDoorTMapper.insertHouseRepairDoorT(houseRepairDoorVO);
        }
        return result;

        }

    /**
     * 修改户资金修改
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHouseRepairDoorT(HouseRepairDoorT houseRepairDoorT, HouseRepairCheckT houseRepairCheckO,HouseProjectT houseProjectT)
    {
        houseRepairDoorTMapper.insertRepairCheck(houseRepairCheckO);
        houseRepairDoorTMapper.updateRepairMoneyForCheck(houseRepairDoorT);
        HouseRepairAreaT repairVO=houseRepairAreaTMapper.sumNoMoney(houseProjectT);
        houseProjectT.setPayMoney(repairVO.getPayMoney());
        return houseRepairDoorTMapper.updateProjectMoneyForCheck(houseProjectT);
    }


    /**
     * 批量删除户资金修改
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteHouseRepairDoorTByDoorIds(String[] houseIds,String projectId)
    {
        int result = 0;
        for(String houseId : houseIds){
            HouseRepairDoorT houseRepairDoorT = new HouseRepairDoorT();
            houseRepairDoorT.setProjectId(projectId);
            houseRepairDoorT.setHouseId(houseId);
            result = houseRepairDoorTMapper.deleteRepairT(houseRepairDoorT);
        }
        return result;
    }

    /**
     * 删除户资金修改信息
     *
     * @param doorId 户资金修改主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteHouseRepairDoorTByDoorId(Long doorId)
    {
        return houseRepairDoorTMapper.deleteHouseRepairDoorTByDoorId(doorId);
    }

    /**
     * 核算维修项目
     *
     * @param projectVO,repairListTemp 核算维修项目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProjectAndRepair(HouseProjectT projectVO, List<HouseRepairDoorT> repairListTemp)
    {
        for(HouseRepairDoorT vo:repairListTemp){
            houseRepairDoorTMapper.updateRepair(vo);
        }
        HouseRepairAreaT projectTemp = new HouseRepairAreaT();
        projectTemp=houseRepairAreaTMapper.sumNoMoney(projectVO);
        projectVO.setNoMoney(projectTemp.getNoMoney());
        return houseRepairDoorTMapper.updateProjectForTrueMoney(projectVO);
    }

    public HouseRepairDoorT selectHouseProjectTByProjectId(HouseRepairDoorT houseRepairDoorT)
    {
        return houseRepairDoorTMapper.selectHouseProjectTByProjectId(houseRepairDoorT);
    }

    @Override
    public HouseRepairDoorT selectRepairQuery(HouseRepairDoorT houseRepairDoorT) {
        return houseRepairDoorTMapper.selectRepairQuery(houseRepairDoorT);
    }

    public HouseRepairDoorT getRepairDetail(HouseRepairDoorT houseRepairDoorT)
    {
        return houseRepairDoorTMapper.getRepairDetail(houseRepairDoorT);
    }

}
